import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

public class Content {

    private File file;
    private Long lastModified = -1L;
    private boolean modified = false;
    private byte[] content = new byte[0];
    public Content(File file) {
        this.file = file;
    }


    public static void getAllClasses(String rootPath, Map<String, Content> classes) {
        File file = new File(rootPath);
        File[] list = file.listFiles();
        for (int i = 0; i < list.length; i++) {
            File f = list[i];
            if (f.isDirectory()) {
                getAllClasses(f.getPath(), classes);
            }

            if (f.getPath().endsWith(".class")) {
                String path = f.getPath();
                String key = path.replace(PathUtils.getRootPath(), "").replace(".class", "");
                Content content;
                if (classes.containsKey(key)) {
                    content = classes.get(key);
                } else {
                    content = new Content(f);
                    classes.put(key, content);
                }
                try {
                    Content.fileContent(content);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }

    }

    public static Content fileContent(Content content) throws Exception {
        File file = content.getFile();
        Long lastModified = content.getLastModified();
        if (lastModified == file.lastModified()) {
            content.setModified(false);
        } else {
            content.setModified(true);
            content.setLastModified(file.lastModified());
            InputStream inputStream = new FileInputStream((file));
            byte[] buffer = new byte[inputStream.available()];
            int read = inputStream.read(buffer);
            if (-1 == read) {
                System.out.println("empty file :" + file.getName());
                content.setContent(null);
            } else {
                content.setContent(buffer);
            }
        }
        return content;
    }

    public boolean isModified() {
        return modified;
    }

    public Content setModified(boolean modified) {
        this.modified = modified;
        return this;
    }

    public File getFile() {
        return file;
    }

    public Content setFile(File file) {
        this.file = file;
        return this;
    }

    public Long getLastModified() {
        return lastModified;
    }

    public Content setLastModified(Long lastModified) {
        this.lastModified = lastModified;
        return this;
    }

    public byte[] getContent() {
        return content;
    }

    public Content setContent(byte[] content) {
        this.content = content;
        return this;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Content{\n");
        sb.append("file=").append(file).append(",\n");
        sb.append("lastModified=").append(lastModified).append(",\n");
        sb.append("modified=").append(modified).append(",\n");
        sb.append("contentLength=").append(content.length).append("\n");
        sb.append("}\n");
        return sb.toString();
    }
}